package com.zc.manager.network.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.zc.component.gateway.DeviceGatewayManager;
import com.zc.manager.network.domain.IotDeviceGateway;
import com.ruoyi.common.core.controller.BaseController;
import com.zc.manager.network.service.IotDeviceGatewayService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 设备网关Controller类
 *
 * @author zhaoxianglong
 * @date 2022-11-7
 */
@RestController
@RequestMapping("/iot/device-gateway")
public class IotDeviceGatewayController extends BaseController {

    @Resource
    private IotDeviceGatewayService iotDeviceService;

    @Resource
    private DeviceGatewayManager gatewayManager;

    /**
     * 查询
     */
    @PreAuthorize("@ss.hasPermi('iot:deviceGateway:list')")
    @GetMapping("/list")
    public TableDataInfo listDeviceGateway(IotDeviceGateway iotDeviceGateway) {
        startPage();
        List<IotDeviceGateway> list = iotDeviceService.selectIotDeviceGatewayList(iotDeviceGateway);
        return getDataTable(list);
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('iot:deviceGateway:add')")
    @Log(title = "新增设备网关", businessType = BusinessType.IMPORT)
    @PostMapping
    public AjaxResult addDeviceGateway(@Valid @RequestBody IotDeviceGateway iotDeviceGateway) {
        return toAjax(iotDeviceService.addDeviceGateway(iotDeviceGateway));

    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('iot:deviceGateway:edit')")
    @Log(title = "修改设备网关", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editDeviceGateway(@RequestBody IotDeviceGateway iotDeviceGateway) {
        return toAjax(iotDeviceService.editDeviceGateway(iotDeviceGateway));
    }

    /**
     * 查询详情
     */
    @PreAuthorize("@ss.hasPermi('iot:deviceGateway:query')")
    @GetMapping("/{id}")
    public AjaxResult getDeviceGateway(@PathVariable Long id) {
        return AjaxResult.success(iotDeviceService.getDeviceGateway(id));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('iot:deviceGateway:remove')")
    @Log(title = "删除设备网关", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult removeDeviceGateway(@PathVariable List ids) {
        return toAjax(iotDeviceService.removeByIds(ids));
    }

    /**
     * 导出文件
     */
    @PreAuthorize("@ss.hasPermi('iot:deviceGateway:export')")
    @Log(title = "导出设备网关", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void exportDeviceGateway(HttpServletResponse response, IotDeviceGateway iotDeviceGateway) {
        List<IotDeviceGateway> list = iotDeviceService.selectIotDeviceGatewayList(iotDeviceGateway);
        ExcelUtil<IotDeviceGateway> util = new ExcelUtil<>(IotDeviceGateway.class);
        util.exportExcel(response, list, "设备网关数据");
    }

    /**
     * 状态修改：启动
     */
    @PreAuthorize("@ss.hasPermi('iot:device:edit')")
    @PostMapping("/startup/{id}")
    public AjaxResult startupDeviceGateway(@PathVariable Long id) {
        return toAjax(iotDeviceService.startupDeviceGateway(id));
    }

    /**
     * 状态修改：停止
     */
    @PreAuthorize("@ss.hasPermi('iot:device:edit')")
    @DeleteMapping("/shutdown/{id}")
    public AjaxResult shutdownDeviceGateway(@PathVariable Long id) {
        return toAjax(iotDeviceService.shutdownDeviceGateway(id));
    }

    /**
     * 获取支持的设备网关
     */
    @PreAuthorize("@ss.hasPermi('iot:deviceGateway:query')")
    @GetMapping("/providers")
    public AjaxResult getProviders() {
        return AjaxResult.success(gatewayManager.getProviders());
    }

}
